<?php
/*
* Class teia\webdev\php\core\CartItem
* Core shopping cart item
*
* @author Bernardo Quibiana
* @date created: 20111122
*
* @date modified: 20111122
* @modified: Bernardo Quibiana
*/

// namespace declaration: Abstract Data Types
namespace teia\webdev\php\core;

require_once 'IDisplayableItem.inc';

class ShoppingCartItem implements IDisplayableItem {
//	private $prod_id;
//	private $item_id;
	private $item_link;
	private $cart_qty;
	private $cart_price;
	
	private $item_obj;
	private $prod_obj;
	
	function __construct($prod_id, $item_id, $cart_qty, $item_link) {
	//	$this->prod_id = $prod_id;
	//	$this->item_id = $item_id;
		$this->item_link = $item_link;
		$this->cart_qty = 0;
		
		$this->prod_obj = Product::db_fetch($prod_id, null, array('item_id' => $item_id));
		$this->item_obj = $this->prod_obj->get_item_obj($item_id);
		$this->add_cart_qty($cart_qty);
	}
	
	public function add_cart_qty($cart_qty) {
		$cart_qty = $this->cart_qty + $cart_qty;
		$item_qty = $this->item_obj->get('item_qty');
		$max_qty = FunctionPool::clamp($item_qty, 1, 30);	// ecom.purchase.max_it_qty = 30
		
		$this->cart_qty = FunctionPool::clamp($cart_qty, 1, $max_qty);
	}
	
	public function set_cart_qty($cart_qty) {
		$item_qty = $this->item_obj->get('item_qty');
		$max_qty = FunctionPool::clamp($item_qty, 1, 30);	// ecom.purchase.max_it_qty = 30
	
		$this->cart_qty = FunctionPool::clamp($cart_qty, 1, $max_qty);
	}
	
	public function get_cart_qty() {
		return $this->cart_qty;
	}
	
	public function get_subtotal() {
		$subtotal = 0.0; 
		$prod_price = $this->prod_obj->get('prod_price');
		$subtotal = $prod_price * $this->cart_qty;
		
		return $subtotal;
	}
	
	public function get_prod_property($property) {
		return $this->prod_obj->get($property);
	}
	
	public function get_item_property($property) {
		return $this->item_obj->get($property);
	}
	
	/*
	* IDisplayableItem
	*/
	public function get_thumbnail() {
		
	}
	
	
	public function get_slide_image() {
	return null;
	}
	
	
	public function get_small_image() {
	return null;
	}
	
	
	public function get_medium_image() {
	
	}
	
	public function get_large_image(){
	
	}
	
	
	public function get_alt_text() {
	
	}
	
	
	public function get_element_id() {
	
	}
	
	public function get_element_class() {
	
	}
	
	public function get_details() {
	
	}
	
	
	public function generate($context) {
		//echo "generating $this->prod_id-$this->item_id x $this->cart_qty <br />";
		$prod_id = $this->prod_obj->get('prod_id');
		$prod_price = $this->prod_obj->get('prod_price');
		$prod_brand = $this->prod_obj->get('prod_brand');
		
		$thumb_img = $this->item_obj->get_thumbnail();
		$item_id = $this->item_obj->get('item_id');
		$item_name = $this->item_obj->get('item_name');
		$item_size = $this->item_obj->get('item_size');
		$item_color = $this->item_obj->get('item_color');
		$item_qty = $this->item_obj->get('item_qty');
		$item_link = $this->item_link;
		
		$max_qty = FunctionPool::clamp($item_qty, 1, 30);	// ecom.purchase.max_it_qty = 30
		$options = FunctionPool::array_to_assoc(range(1, $max_qty), false);
		$qty_selector = HtmlUtil::generate_select($options, $this->cart_qty, null, 'cart_qty');
		
		$subtotal = $this->get_subtotal();
		$queries = array('action'=>'rm', 'prod_id' => $prod_id, 'item_id' => $item_id);
		$rm_link = LinkUtil::generate_javascript_link($queries, true);
		
		$content = "<div class=\"cart_it_content\">\n";
		
		$content .= "<div class=\"cart_it_thumbnail\">\n";
		$content .= "<a href=\"$item_link\"><img src=\"$thumb_img\" /></a>";
		$content .= "</div>\n";
		
		$content .= "<div class=\"cart_it_description\">\n";
		$content .= "<p>$item_name</p>\n";
		$content .= "<hr />";
		$content .= "<p><span>Marca:</span>$prod_brand</p>\n";
		$content .= "<p><span>Cor:</span>$item_color</p>\n";
		$content .= "<p><span>Tamanho:</span>$item_size</p>\n";
		$content .= "<p><span>ID:</span>$item_id</p>\n";
		$content .= "</div>\n";
		
		$content .= "<div class=\"cart_it_pricing\">\n";
		$content .= "<p><span>Price:</span>\$$prod_price</p>\n";
		$content .= "<p><span>Quantidade:</span>$qty_selector</p>\n";
		$content .= "<p><span>Subtotal:</span>\$$subtotal</p>\n";
		$content .= "<br /><button class=\"button_rm\" type=\"button\" onclick=\"$rm_link\">Remover</button>\n";
		$content .= "</div>\n";
		
		$content .= "</div>\n";
		
		//echo "generating $this->prod_id-$this->item_id x $this->cart_qty <br />";
		return $content;
	}
	
	public function display($context) {
		echo $this->generate($context);
	}
}
?>